package com.chazz.servlet;

import com.chazz.entity.User;
import com.chazz.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;

@WebServlet("/testLogin.do")
public class login extends HttpServlet {
    UserServiceImpl usi=new UserServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        String userEmail=req.getParameter("email");
        String userPassword=req.getParameter("password");
           User user=usi.findUserByEmail(userEmail);
           if(user.getPassword().equals(userPassword)){
               HttpSession session=req.getSession();
               session.setAttribute("user",user);
               //resp.sendRedirect("personalCenter.jsp");
               System.out.println("登录成功");
               resp.sendRedirect("chatClient.jsp");
           }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
}
